package com.yuchuang.controller;

import com.yuchuang.base.PageInfo;
import com.yuchuang.base.Result;
import com.yuchuang.model.Spec;
import com.yuchuang.model.vo.SpecVo;
import com.yuchuang.service.SpecService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;
import java.util.Map;

/**
 *@authoe    :cy
 *@date      :13:50 2023-04-25
 *description:  规格信息相关接口
 *return     :
 **/
@RestController
@RequestMapping("/spec")
@Api(tags="规格信息")
public class SpecController {
    @Autowired
    private SpecService specService;
    @GetMapping("all")
    @ApiOperation("查询所有")
    @ApiImplicitParams({
            @ApiImplicitParam(value="{\n" +
                    "    \"specName\":1,  //可选  varchar  模糊查询所有 \n" +
                    "}",name = "Map",required = true,paramType = "body"),
    })
    public Result all(HttpServletRequest request){
        try{
            List list = specService.queryAll(request);
            return Result.ok(list);
        }catch(Exception e){
            return Result.fail("查询失败"+e);
        }
    }
    //Mybatisplus  分页查询
    @PostMapping("/selectAll")
    @ApiOperation("MybatisPlus 分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(value="{\n" +
                    "    \"pageNum\":1,\n" +
                    "    \"pageSize\":\"10\"\n" +
                    "    \"specName\":1,  //可选  varchar  模糊查询所有 \n" +
                    "}",name = "Map",required = true,paramType = "body"),
    })
    public Result selectAll(
            @ApiIgnore @RequestBody SpecVo specVo
    ){
        PageInfo pageInfo = specService.selectPageVo(specVo);
        return Result.ok("查询成功",pageInfo);
    }
    @PostMapping("/insert")
    @ApiOperation("新增")
    @ApiImplicitParams({
            @ApiImplicitParam(value= "{\n" +
                    "\"specName\":\"null\",// 必填 varchar  规格名称 \n" +
                    "\"specDescribe\":\"null\", //必填 varchar   规格描述 \n" +
                    "}",name = "Map",required = true,paramType = "body"),
    })
    public Result insert(
            @ApiIgnore  @RequestBody Spec spec
    ){
        try{
            int val = specService.insert(spec);
            return Result.ok("添加成功",val);
        }catch(Exception e){
            Throwable cause = e.getCause();
            String errMsg = "";
            if(cause instanceof SQLIntegrityConstraintViolationException){
                //错误信息
                errMsg = ((SQLIntegrityConstraintViolationException) cause).getMessage();
                //根据约束名称定位具体字段
                if(StringUtils.isNotBlank(errMsg) && errMsg.indexOf("specName_unique") != -1){
                    return Result.fail("重复添加失败");
                }else{
                    return Result.fail("添加失败"+errMsg);
                }
            }else{
                return Result.fail("添加失败"+e);
            }
        }
    }
    @PostMapping("/update")
    @ApiOperation("修改")
    @ApiImplicitParams({
            @ApiImplicitParam(value="{\n" +
                    "\"id\":\"1\"\n" +
                    "\"specName\":\"null\",// 必填 varchar  规格名称 \n" +
                    "\"specDescribe\":\"null\", //必填 varchar   规格描述 \n" +
                    "}",name = "Map",required = true,paramType = "body"),
    })
    public Result update(
            @ApiIgnore  @RequestBody Map<String,Object> params
    ){
        try{
            int val = specService.update(params);
            return Result.ok("修改成功",val);
        }catch (Exception e){
            Throwable cause = e.getCause();
            String errMsg = "";
            if(cause instanceof SQLIntegrityConstraintViolationException){
                //错误信息
                errMsg = ((SQLIntegrityConstraintViolationException) cause).getMessage();
                //根据约束名称定位具体字段
                if(StringUtils.isNotBlank(errMsg) && errMsg.indexOf("specName_unique") != -1){
                    return Result.fail("重复数据！修改失败");
                }else{
                    return Result.fail("修改失败"+errMsg);
                }
            }else{
                return Result.fail("修改失败"+e);
            }
        }
    }
    @PostMapping("/delete")
    @ApiOperation("删除操作")
    @ApiImplicitParams({
            @ApiImplicitParam(value="{\n" +
                    "    \"id\":\"1\"\n" +
                    "}",name = "Map",required = true,paramType = "body"),
    })
    public Result delete(
            @ApiIgnore  @RequestBody Map<String,Object> params
    ){
        try{
            int val = specService.delete(params);
            return Result.ok("删除成功",val);
        }catch(Exception e){
            return Result.fail("删除失败"+e);
        }
    }
}
